package com.example.team.rest.common.datasources;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataSourceContextHolder {

    private static final Logger LOG = LoggerFactory.getLogger(DataSourceContextHolder.class);

    /**
     * 默认数据源
     */
    public static final String FIRST_DS = "first";

    /**
     * 线程本地环境
     */
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    /**
     * 设置数据源名
     * @param datasourceType
     */
    public static void setDatasource(String datasourceType) {
        LOG.debug("切换到{}数据源", datasourceType);
        contextHolder.set(datasourceType);
    }

    /**
     * 获取数据源名
     * @return
     */
    public static String getDatasource() {
        return (contextHolder.get());
    }

    /**
     * 清除数据源名
     */
    public static void clearDB() {
        contextHolder.remove();
    }
}
